package com.tubi.maipiao2;

public class MyRunnable implements Runnable {
    public  static   int tickcound = 100;


    @Override
    public void run() {
        while (true){
            if("窗口一".equals(Thread.currentThread().getName())){
                synchronizedMthod();
            }
            if ("窗口二".equals(Thread.currentThread().getName())){
                synchronized (MyRunnable.class){
                    if(tickcound == 0){
                        break;
                    }else {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        tickcound--;
                        System.out.println(Thread.currentThread().getName() + "在买票，还剩下"+tickcound+'张');
                    }
                }
            }
        }
    }

    private static synchronized boolean synchronizedMthod() {
        if(tickcound == 0){
            return true;
        }else {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tickcound--;
            System.out.println(Thread.currentThread().getName() + "在买票，还剩下"+tickcound+'张');
            return false;
        }
    }
}
